#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
struct stu
{
	char name[20];
	int age;
};

int cmp_by_age(const void* p1, const void* p2)
{
	return (((struct stu*)p1)->age - ((struct stu*)p2)->age);
}

int cmp_by_name(const void* p1, const void* p2)
{
	return strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name);
}

void swap(char* begin, int sz)
{
	for (int i = 0; i < sz; i++)
	{
		char tmp = 0;
		tmp = begin[sz + i];
		begin[sz + i] = begin[i];
		begin[i] = tmp;
	}
}

void bubble_sort(void* base, size_t num, size_t sz, int(*compar)(const void*, const void*))
{
	for (int i = 0; i < num - 1; i++)
	{
		for (int j=0;j<num-1-i;j++)
		{
			if (compar((char*)base + j * sz, (char*)base + (j + 1) * sz) > 0)
			{
				swap((char*)base + j * sz, sz);
			}
		}
	}
}

int main()
{
	struct stu arr[3] = { {"zhangsan",20},{"lisi",15},{"wangwu",25} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	bubble_sort(arr, sz, sizeof(arr[0]), cmp_by_age);
	return 0;
}



